package code13.set;

import java.util.Arrays;
import java.util.Comparator;
import java.util.HashSet;
import java.util.Random;
import java.util.TreeSet;

import org.junit.Test;

public class Test01 {
	@Test
	public void f1() {
		HashSet<String> set = new HashSet<String>();
		// 存入(无序的, 和存入顺序不一定相同)
		set.add("a");
		set.add("b");
		set.add("c");
		set.add("Z");
		set.add("Y");
		
		System.out.println(set);
		System.out.println("-------------");
		// 常用方法:
		// size()、isEmpty()、clear()、remove(Object o)、
		// contains()、iterator()、toArray()
		// 遍历:
		// foreach / iterator / forEach()
	}
	@Test
	public void f2() {
		HashSet<Stu> set = new HashSet<Stu>();
		set.add(new Stu(1, "tom", 1));
		set.add(new Stu(2, "jerry", 2));
		set.add(new Stu(1, "tom", 3));
		System.out.println(set);
	}
	@Test
	public void t1() {
		// 1.随机10个学生的姓名并存入一个数组, 学生的姓名 由 26小写字母中的两个随机组成.
		Random r = new Random();
		String[] arr = new String[10];
		for (int i = 0; i < arr.length; i++) {
			char c1 = (char) (r.nextInt(26) + 'a');
			char c2 = (char) (r.nextInt(26) + 'a');
			// String name = "" + c1 + c2;
			arr[i] = new String(new char[]{ c1, c2 });
		}
		System.out.println("arr:" + Arrays.toString(arr));
		//arr = new String[]{"ab","wd","sf","ab","wd","fj","uk","so","xv","fj"};
		
		// 2.去除重复的学生姓名.
		HashSet<String> set = new HashSet<String>();
		for (String name : arr) {
			set.add(name);
		}
		System.out.println("names:"+set);
		// 3.统计这些学生姓名一共使用了哪些字母.
		HashSet<Character> words = new HashSet<Character>();
		for (String name : arr) {
			words.add(name.charAt(0));
			words.add(name.charAt(1));
		}
		System.out.println("这些学生姓名一共使用了哪些字母:"+words);
	}
	
	@Test
	public void f3() {
		// 1.元素自身可排序
		TreeSet<String> set = new TreeSet<String>();
		set.add("def");
		set.add("qwe");
		set.add("abc");
		set.add("abcd");
		set.add("dwq");
		System.out.println(set);
	}
	@Test
	public void f4() {
		Comparator<Stu> comparator = new Comparator<Stu>() {
			// 参数1 大于 参数2, 返回 >0 的值
			// 参数1 小于 参数2, 返回 <0 的值
			// 参数1 等于 参数2, 返回 0
			@Override
			public int compare(Stu o1, Stu o2) {
				// 根据 String 的 比较排序(去重)
				int ct = o1.name.compareTo(o2.name);
				return ct == 0 ? o1.id - o2.id : ct;
			}
		};
		// 指定比较规则
		TreeSet<Stu> set = new TreeSet<Stu>(comparator);
		set.add(new Stu(1, "tom", 1));
		set.add(new Stu(2, "jerry", 2));
		set.add(new Stu(3, "tom", 1));
		System.out.println(set);
	}
}

class Stu {
	public int id;
	public String name;
	public int num;
	public Stu(int id, String name, int num) {
		this.id = id;
		this.name = name;
		this.num = num;
	}
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + id;
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		return result;
	}
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Stu other = (Stu) obj;
		if (id != other.id)
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		return true;
	}
	@Override
	public String toString() {
		return "Stu [id=" + id + ", name=" + name + ", num=" + num + "]";
	}
}